<?php

function draw_pagenumbers($startpage,$entries,$entries_per_page,$page_uri,$varname) {
	$startpage = intval($startpage);
	$entries   = intval($entries);

	if ($entries_per_page <= 0) $entries_per_page = 25;

	$last = $entries % $entries_per_page;
	$max_page =  (int)($entries / $entries_per_page);
	if ($last > 0) $max_page++;	 // max. Anzahl von Seiten
	$prepages = $startpage-1;
	if ($prepages > 5) $prepages = 5;
	$followpages = 10-1-$prepages;
	if ($followpages > 9) $followpages = 9;
	if (($followpages + $startpage) > $max_page) $followpages = $max_page - $startpage;
	if (($prepages + $followpages + 1) < $max_page) $prepages = 10 - $followpages - 1;
	if ($prepages >= $startpage) $prepages = $startpage - 1; // neu

	$endpage = $startpage+$followpages;
	$urlbind = strpos($page_uri,"?"); // Schauen ob schon Variablen angeh�ngt sind
	if ($urlbind === FALSE) {
		$urlbind = "?";
	} else {
		$urlbind = "&amp;";
	}
	// pagelink generieren
	$page_uri  = str_replace($urlbind.$varname."=".$startpage,"",$page_uri);

	// < und << davorsetzen
	if ((($startpage - 10) <= 0) && ($startpage > 2)) {
		echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=1\">&lt;&lt;</a>&nbsp;";
	} elseif ((($startpage - 10) > 0)) {
		echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=".($startpage-10)."\">&lt;&lt;</a>&nbsp;";
	}
	if ($startpage > 1) {
		echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=".($startpage-1)."\">&lt;</a>&nbsp;";
	}

	// Seitennummern inkl. pagelink ausgeben
	$pagenumber = $startpage - $prepages;
	while ($pagenumber <= $endpage) {
		if ($pagenumber != $startpage) {
			echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=".$pagenumber."\">$pagenumber</a>&nbsp;";
		} else {
			echo "<span class=\"currentpage\">".$pagenumber."&nbsp;</span>";
		}
		$pagenumber++;
	}

	// > und >> dahintersetzen
	if ($startpage < $max_page) {
		echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=".($startpage+1)."\">&gt;</a>&nbsp;";
	}
	if ((($startpage + 10) >= $max_page) && ($startpage+1 < $max_page)) {
		echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=".$max_page."\">&gt;&gt;</a>&nbsp;";
	} elseif ((($startpage + 10) < $max_page)) {
		echo "<a class=\"pagelink\" href=\"".$page_uri.$urlbind.$varname."=".($startpage+10)."\">&gt;&gt;</a>&nbsp;";
	}


}



function makeheader($title) {
	global $additional_links_english,$additional_links_chinese,$ostat_universe,$default_charset,$email_from;
	if (!isset($additional_links_english) || !is_array($additional_links_english)) include_once "attributes.php";
	if (!isset($additional_links_chinese) || !is_array($additional_links_chinese)) include_once "attributes.php";

	// styles path holen
	include "config.php";
	mysql_connect($dbhost,$dbusername,$dbpassword);
	mysql_select_db($dbname);
	$query = "SELECT stylepath FROM $utablename WHERE id='".$_SESSION['s_userid']."'";
	$res = mysql_query($query) or die(mysql_error());
	$line = mysql_fetch_object($res);
	$stylepath = $line->stylepath;
	if (trim($stylepath) == "") $stylepath = "styles.css";

	$tmp = strrpos($stylepath,"/");
	$modname = "EpicBlue";
	if ($tmp !== false) {
		$tmp = substr($stylepath,0,$tmp);
		$pos = strrpos($tmp,"/");
		if ($pos !== false) $modname = substr($tmp,$pos+1,strlen($tmp)-$pos);
	}


	echo '
	<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
	<html>
	<head>
	<meta http-equiv="content-type" content="text/html; charset='.$default_charset.'">
	<title>'.$title.'</title>
	<link rel="stylesheet" type="text/css" href="'.$stylepath.'" />
	</head>
	<body>
	<table cellspacing="0" cellpadding="0" style="width:930px;" border="0" class="frameset">
	  <tr>
	    <td valign="top" style="width:172px;">
		<div class="copyright" style="width:120px; margin-left:26px; margin-top:26px;" align="center">'.SHORTNAME.'<br>';
	// --------------------------------- BEGIN OF INFORMATION BLOCK ---------------------------------------------
	$universe = $_SESSION['s_universe'];
	if (empty($universe)) $universe = $ostat_universe;
	if (!empty($universe) && is_numeric($universe)) echo '<span class="mod">'.UNIVERSE.' '.$universe.'</span><br>';
	echo '<span class="mod">'.$modname.'-Mod</span></div>
	    <table class="menu" cellspacing="1" cellpadding="1" width="100%" border="0">
	    <tr><td class="tblhead">'.MENU_INFO_LINKS.'</td></tr>
	    <tr class="firstcolor"><td class="menu"><a class="menulink" href="index.php">'.MENU_PAGE.'</a></td></tr>'."\n";
	if ($_SESSION['s_cansearch']) {
		echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="show.php">'.MENU_DB_SEARCH.'</a></td></tr>'."\n";
		if($_SESSION['s_caninsert']) {
			echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="notices.php?action=search">'.MENU_DB_NOTICES.'</a></td></tr>'."\n";
		}
	}
	if ($_SESSION['s_probeview']) echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="reports.php?action=search">'.MENU_DB_REPORTS.'</a></td></tr>'."\n";
	if ($_SESSION['s_cansearch']) {
		if (isset($_SESSION['s_gv_galaxy']) && isset($_SESSION['s_gv_system']))
		echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="view.php?gala='.$_SESSION['s_gv_galaxy'].'&amp;system='.$_SESSION['s_gv_system'].'">'.MENU_GALAXYVIEW.'</a></td></tr>'."\n";
		else
		echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="view.php?gala=1&amp;system=1">'.MENU_GALAXYVIEW.'</a></td></tr>'."\n";
	}
	
	echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="combatview.php">'.MENU_BATTLEVIEW.'</a></td></tr>'."\n";
	echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="pptrendsview.php">'.MENU_RANKVIEW.'</a></td></tr>'."\n";
	echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="stats.php">'.MENU_STATISTICS.'</a></td></tr>'."\n";
	/*
	echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="allyhistory.php">'.MENU_ALLYHISTORY.'</a></td></tr>'."\n";
	*/
	echo "</table>";

	// --------------------------------- END OF INFORMATION BLOCK ---------------------------------------------


	// --------------------------------- BEGIN OF OPTION BLOCK ---------------------------------------------
	echo '<table class="menu" cellspacing="1" cellpadding="1" width="100%" border="0">
    	  <tr><td class="tblhead">'.MENU_OPTION_LINKS.'</td></tr>';

	if ($_SESSION['s_caninsert']) echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="einlesen.php">'.MENU_DB_REFRESH.'</a></td></tr>'."\n";

	if ($_SESSION['s_statusview']) {
		echo '    <tr class="firstcolor"><td class="menu"><a class="menulink" href="status.php">'.MENU_DB_STATUS.'</a></td></tr>'."\n";
	}

	if ($_SESSION['s_userman']) {
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="usermanagement.php">'.MENU_USEROPTIONS.'</a></td></tr>'."\n";
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="userinfo.php">'.MENU_USEROPTIONS3.'</a></td></tr>'."\n";
	}
	else if(!$_SESSION['s_isviewer']) {
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="change_password.php">'.MENU_USEROPTIONS2.'</a></td></tr>'."\n";
	}
	
	if($_SESSION['s_caninsert']) {
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="notices.php?action=show">'.MENU_NOTICES.'</a></td></tr>'."\n";
	}

	// --------------------------------- END OF OPTION BLOCK ---------------------------------------------

	// --------------------------------- BEGIN OF STUFF BLOCK ---------------------------------------------
	echo '
	    </table>
	    <table class="menu" cellspacing="0" cellpadding="0" width="100%" border="0">
	    <tr><td class="tblhead">'.MENU_STUFF.'</td></tr>
	    <tr class="firstcolor"><td class="menu"><a class="menulink" href="http://www.utblog.com/plog/66/article/1044" target="_blank">'."Tutorial".'</a></td></tr>
	    <tr class="firstcolor"><td class="menu"><a class="menulink" href="http://www.utblog.com/plog/ogame" target="_blank">'.MENU_UTBLOG_OGAME.'</a></td></tr>
	    <tr class="firstcolor"><td class="menu"><a class="menulink" href="http://www.galaxytool.eu" target="_blank">'.MENU_DERIVE_FROM.'</a></td></tr>'."\n";

	$i = 0;
	if ($_SESSION['lang'] == "english") {
		foreach($additional_links_english as $text => $link)
		if ($i%2==0)
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="'.$link.'" target="_blank">'.$text.'</a></td></tr>'."\n";
		else
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="'.$link.'" target="_blank">'.$text.'</a></td></tr>'."\n";
	} else /* if ($_SESSION['lang'] == "chinese") */ {
		foreach($additional_links_chinese as $text => $link)
		if ($i%2==0)
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="'.$link.'" target="_blank">'.$text.'</a></td></tr>'."\n";
		else
		echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="'.$link.'" target="_blank">'.$text.'</a></td></tr>'."\n";
	}
	echo '<tr class="firstcolor"><td class="menu"><a class="menulink" href="mailto:' . $email_from . '" target="_blank">contact</a></td></tr>
	    </table>
	    <table class="menu" cellspacing="0" cellpadding="0" width="100%" border="0">
	    <tr><td class="tblhead">'.MENU_LANGUAGE.'</td></tr>
	    <tr class="firstcolor"><td align="center">
	    <form action="index.php" method="GET">
	    <select class="pulldown_code" name="language">
          <option value="english">English</option>
          <option value="chinese">Chinese</option>
          </select>&nbsp;
          <input class="button" type="submit" name="OK" value="OK" />
          </form>
          </td></tr>
	    <tr class="firstcolor"><td class="menu""><a class="menulink" href="../index.php?logout">Logout</a></td></tr>
	    </table>
	    </td>
	    <td valign="top" class="mainframe" style="width:628px;">
	    ';
	// --------------------------------- END OF STUFF BLOCK ---------------------------------------------
}


function makefooter() {
	echo '
	    </td>
	    <td style=" padding-top:50px;width:130px;">&nbsp;</td>
	  </tr>
	</table>
	<script language="JavaScript" src="wz_tooltip.js"></script>
	</body>
	</html>';
}


/**
 * This function will check every status and print the letters given as parameters instead. This makes
 * sure that every language will get it's corresponding letter for the status.
 *
 * @param boolean $statusstring Angabe ob es als BB Code ausgegeben werden soll, oder nicht
 * @param string $statusstring
 * @param char(1) $printbanned the letter which shall be shown for banned players
 * @param char(1) $printvaction the letter which shall be shown for players at vacation mode
 * @param char(1) $printnoob the letter which shall be shown for noob players
 * @param char(1) $printinactive the letter which shall be shown for inactive players
 * @param char(1) $printlonginactive the letter which shall be shown for long inactive players
 */
function print_status($show_as_bb_code, $statusstring, $printbanned="g", $printvaction="u", $printnoob="n", $printinactive="i", $printlonginactive="I", $printsuperman="s") {
	$show_as_bb_code = false;
	if (!$show_as_bb_code) {
		if (strpos($statusstring,"g") !== false) {
			echo "<span class=\"banned\">$printbanned</span>";
		}
		if (strpos($statusstring,"u") !== false) {
			echo "<span class=\"vacation\">$printvaction</span>";
		}
		if (strpos($statusstring,"n") !== false) {
			echo "<span class=\"noob\">$printnoob</span>";
		}
		if (strpos($statusstring,"i") !== false) {
			echo "<span class=\"longinactive\">$printinactive</span>";
		}
		if (strpos($statusstring,"I") !== false) {
			echo "<span class=\"inactive\">$printlonginactive</span>";
		}
		if (strpos($statusstring,"s") !== false) {
			echo "<span class=\"superman\">$printsuperman</span>";
		}		
	}
}
?>
